package wta.cloud.action;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wta.cloud.service.Risk;

@Controller
public class RiskAction {
	
	
	
	/*饮酒项： 过量(高度白酒每天大于1两)  为风险项；
	是否吸烟项：是  为风险项；
	每周锻炼次数：<=2次  为风险项；
	其他：是 为风险项。*/
	
	@RequestMapping(value = "/riskAgain", produces = "application/json;charset=UTF-8")  
	@ResponseBody
	public String RiskAgain(HttpSession session,
			@RequestParam(value = "other11", required = false)String other11,
			@RequestParam(value = "bmi24", required = false)String bmi24,
			@RequestParam(value = "other4", required = false)String other4,
			@RequestParam(value = "other5", required = false)String other5,
			@RequestParam(value = "other10", required = false)String other10){
		int[] other = new int[12];
		Integer sex = (Integer)session.getAttribute("sex");
		Integer age = (Integer)session.getAttribute("age");
		Double bmi = (Double)session.getAttribute("bmi");
		int[] n = (int[])session.getAttribute("other");
		other = Arrays.copyOf(n, n.length);
		
		if ("cb1".equalsIgnoreCase(other11))
			other[11]=0;
		if ("cb2".equalsIgnoreCase(bmi24))
			bmi=20.0;
			
		if ("cb3".equalsIgnoreCase(other4))
			other[4]=0;
		if ("cb4".equalsIgnoreCase(other5))
			other[5]=0;
		if ("cb5".equalsIgnoreCase(other10))
			other[10]=0;
		
		Integer newrisk = getBarHeight(Risk.getRisk(sex, age, bmi, other));
		return newrisk.toString();
		
	}
	@RequestMapping("/risk")
	public String RiskResult(HttpSession session,Model model,
			Integer sex, Integer age, Double high, Double weight, 
			Integer qinshubp, Integer bphigh, Integer jiujing, Integer xian, Integer shousun, Integer sugar,
			Integer danguchun, Integer ganyou, Integer duanlian, Integer xiyan){
	//System.out.println(sex+age+high+weight+qinshubp);
		int[] other = new int[12];
		high /=100;
		Double bmi = weight / high / high;
		other[2]  = qinshubp;
		other[3]  = bphigh;
		other[4]  = jiujing;
		other[5]  = xian;
		other[6]  = shousun;
		other[7]  = sugar;
		other[8]  = danguchun;
		other[9]  = ganyou;
		other[10] = duanlian;
		other[11] = xiyan;
		
		session.setAttribute("sex", sex);
		session.setAttribute("age", age);
		session.setAttribute("bmi", bmi);
		session.setAttribute("other", other);
	
	//System.out.println(other.toString());
		
		Double risk = Risk.getRisk(sex, age, bmi, other);
		String riskTip = Risk.getRiskTip(risk);
		
		//获得最小的风险
		double minRisk = Risk.getMinRisk(sex, age, bmi, other);
		
		session.setAttribute("risk", riskTip);
		session.setAttribute("nowRiskHeight", getBarHeight(risk));
		session.setAttribute("minRiskHeight", getBarHeight(minRisk) -10);

		
	//System.out.println(risk);
	//System.out.println(minRisk);
		
		return "yuceresult";
	}
	
	public int getBarHeight(double risk){
		if (risk >= 100) risk = 99.999999;
		else if (risk <= 0) risk = 0.000001;
		
		int h = 0;
		
		//神奇的算式
		//Line Height : 2, 62, 122, 182, 242, 302
		int[] line = {2, 62, 122, 182, 242, 302};
		double[] value = {0, 0.5, 0.9, 1.1, 2.0, 100};

		for (int i = 0; i < 5; i++) {
			if (risk >= value[i] && risk <= value[i+1]){
				//This is magic;
				h = (int) ((risk - value[i]) / (value[i+1] - value[i]) * (line[i+1] - line[i]) + line[i]);
				return h;
			}
		}
		
		return -1;
	}

}